<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/article/SoftUtil.class.php
 *
 * 软件下载的工具类
 *
 * PHP Version 5
 *
 * @package:   class.article
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

//包含需要使用的函数
include_once FUNCTION_PATH.'utf8_substr.fun.php';
include_once FUNCTION_PATH.'ConvertString.fun.php';

//定义处理的类

class SoftUtil {
   
   /**
    * 取得所有的软件分类
    * @param:  &$db 数据库的连接
    * @return: &$category 返回后的数据库的连接
    * @access: public
    * @static
    */
   public static function &getCategory(&$db) {/*{{{*/
      $sql = 'select id, type from soft_type order by id desc ';

      $sth = $db->prepare($sql);
      $res = $db->CacheExecute(1200, $sth);

      $category = array();

      while ( $rows = $res->FetchRow() ) {
         $category[] = array (
            'id'=>$rows['id'],
            'name'=>$rows['type']
         );
      }

      return $category;
   }/*}}}*/


   /**
    * 取得当前分类下的软件所有的页面
    * @param:  &$db 数据库链接
    * @param:  $pre_page 每页显示的数据
    * @param:  $id   软件分类的id
    * @return: $total_page 总共的页数
    * @access: public
    * @static
    */
   public static function getAllPage(&$db, $pre_page = 10, $id=0 ) {/*{{{*/
      $sql = 'select count(*) as num from soft ';
      if ( $id ) {
         $sql .= ' where type=?';
      }
      $sth = $db->prepare($sql);

      $res = null;

      if ( $id ) {
         $res = $db->CacheExecute(30, $sth, array($id));
      } else {
         $res = $db->CacheExecute(30, $sth);
      }

      $rows = $res->FetchRow();

      $total_page = ceil( $rows['num'] / $pre_page );

      return $total_page;
   }/*}}}*/

   /**
    * 取得当前的软件列表
    * @param:  &$db 数据库链接
    * @param:  $pre_page 每页显示的数据记录。
    * @param:  $offset_page 偏移的位置
    * @param:  $id   软件的分类
    * @return: $soft_list 数组列表
    * @access: public
    * @static
    */
   public static function &getSoftList(&$db, $pre_page = 10, $offset_page = 0, $id = 0 ) {/*{{{*/
      $sql = 'select id, name, version, website, downloadurl, comment, downloads from soft ';

      if ( $id ) {
         $sql .= ' where type=? ';
      }

      $sql .= ' order by id desc ';

      $res = null;

      if ( $id ) {
         $res = $db->CacheSelectLimit(120, $sql, $pre_page, $offset_page, array($id));
      } else {
         $res = $db->CacheSelectLimit(120, $sql, $pre_page, $offset_page);
      }

      $soft_list = array();

      while ( $rows = $res->FetchRow() ) {

         $image = self::getOneImage($db, $rows['id']);

         $soft_list[] = array (
            'id'=>$rows['id'],
            'name'=>$rows['name'],
            'version'=>$rows['version'],
            'website'=>$rows['website'],
            'downloadurl'=>$rows['downloadurl'],
            'scomment'=>ConvertString(utf8_substr($rows['comment'], 0, 85), ROOT_URL, IMAGE_URL.'express/'),
            'comment'=>ConvertString($rows['comment'], ROOT_URL, IMAGE_URL.'express/'),
            'downloads'=>$rows['downloads'],
            'img'=>$image
            );
      }

      return $soft_list;

   }/*}}}*/


   /**
    * 得到软件的 一幅图像
    * @param:  &$db  数据库链接
    * @param:  $id 软件的id
    * @return: $image   第一副图像。
    * @access: public
    * @static
    */
   public static function getOneImage(&$db, $id) {/*{{{*/
      $sql = 'select image from soft_image where soft=? order by id asc';
      $res = $db->CacheSelectLimit(60, $sql, 1, 0, array($id));
      $rows = $res->FetchRow();

      return $rows['image']?ROOT_URL.'upload/soft/image/'.$rows['image']:IMAGE_URL.'softdefault.jpg';

   }/*}}}*/


   /**
    * 判断软件是否存在
    * @param: &$db 数据库的链接
    * @param:  $id 软件的id
    * @return: $exists  
    * @access: public
    * @static
    */
   public static function isExists(&$db, $id) {/*{{{*/
      $sql = 'select count(*) as num from soft where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));

      $rows = $res->FetchRow();

      if ( $rows['num'] ) {
         return true;
      } else {
         return false;
      }
   }/*}}}*/
   
   /**
    * 根据软件的id而取得软件的基本信息
    * @param:  &$db 数据库链接
    * @param:  $id   软件的id
    * @return: $soft_info 软件类表
    * @access: public
    * @static
    */
   public static function &getSoftInfo(&$db, $id) {/*{{{*/
      $sql = 'select a.id, a.name, a.version, a.website, a.downloadurl, a.comment, a.downloads '.
         ', b.type from soft a join soft_type b on a.type=b.id  '.
         ' where a.id=?';

      $res = $db->CacheExecute(60, $sql, array($id));
      $rows = $res->FetchRow();

      $soft_info = array (
         'id'=>$rows['id'],
         'name'=>$rows['name'],
         'version'=>$rows['version'],
         'website'=>$rows['website'],
         'downloadurl'=>$rows['downloadurl'],
         'comment'=>ConvertString($rows['comment'],  ROOT_URL, IMAGE_URL.'express/'),
         'downloads'=>$rows['downloads'],
         'type'=>$rows['type'],
      );

      return $soft_info;
   }/*}}}*/
   
   /**
    * 取得软件的所有图片 
    * @param:  &$db 数据库链接
    * @param:  $id   软件的id
    * @return: $image 图像的数组
    * @access: public
    * @static
    */
   public static function &getImage(&$db, $id) {/*{{{*/
      $sql = 'select image from soft_image where id=? order by id desc ';

      $res = $db->CacheExecute(120, $sql, array($id));

      $image = array();

      while ( $rows = $res->FetchRow() ) {
         $image[] = array (
            'img' => $rows['image']?ROOT_URL.'upload/soft/image/'.$rows['image']:IMAGE_URL.'softdefault.jpg'
         );
      }

      return $image;

   }/*}}}*/

   /**
    * 取得软件的评论
    * @param: &$db 数据库链接
    * @param:  $id   软件的id
    * @return: $soft_comment 软件评论的数组
    * @access: public
    * @static
    */
   public static function &getsoftcomment(&$db, $id) {/*{{{*/
      $sql = 'select id, author, postdate, content from soft_comment where soft=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));

      $soft = array();

      while ( $rows = $res->FetchRow() ) {
         $soft[] = array (
            'id'=>$rows['id'],
            'author'=>$rows['author'],
            'date'=>$rows['postdate'],
            'content'=>ConvertString($rows['content'], ROOT_URL, IMAGE_URL.'express/'),
         );
      }
      return $soft;

   }/*}}}*/





}

?>
